﻿'use strict';
app.controller('PayOrderController',
    [
    '$rootScope',
    '$scope',
    '$localStorage',
    '$state',
    '$stateParams',
    '$modal',
    'BitcoinFactory', '$filter', 'commonFactory',
function ($rootScope, $scope, $localStorage, $state,$stateParams, $modal, BitcoinFactory, $filter) {
    $scope.title = "Payment Order",
    $scope.messeger = "",
    $scope.showQRCode = false,
    $scope.orderId = $stateParams.orderId,
    $scope.userRecieve = $stateParams.userRecieve,
    $scope.showFormBitcoin = true,
    $scope.BitcoinAccount = {
        Amount: 0,
        Address: '',
        Fee: 0,
        BCTransactionId: 0,
        Data: function () { return "bitcoin:" + this.Address + "?amount=" + this.Fee * this.Amount }
    },
    $scope.popup = function (windowClass, StyleModal, Controller, size) {
        $modal.open({
            windowClass: windowClass,
            templateUrl: StyleModal,
            controller: Controller,
            size: size,
            resolve: {
                messeger: function () {
                    return $scope.messeger;
                },
                title: function () {
                    return $scope.title;
                }
            }
        })
    },
   $scope.BitcoinAccount.Amount = $stateParams.amount,
    //GetOrderById($scope.orderId),
   GetBitcoinOfUserReceive($scope.userRecieve),
   //getBitcoinOfUserLogin($localStorage.user.UserId),
   GetBitcointoUSDRate(1);
    function GetBitcoinOfUserReceive(userRecieveID) {
       BitcoinFactory.getBitcoinOfUserReceive(userRecieveID).then(function (response)
       {
           if (response.data.Status) {
               //$scope.UserName = response.data.UserName;
               //$scope.FullName = response.data.FullName;
               //$scope.AddressRecieve = response.data.Payer;
               $scope.BitcoinAccount.Address = response.data.Payer;
               $scope.showQRCode = true;
           } else {
               $scope.message = response.data.Messeger;
           }
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
   };
   //function getBitcoinOfUserLogin(UserId) {
   //    BitcoinFactory.getBitcoinOfUserReceive(UserId).then(function (response)
   //    {
   //        if (response.data.Staus) {
   //            $scope.BitcoinAccount.Address = response.data.Payer;
   //            $scope.showQRCode = true;
   //        } else {
   //            $scope.message = response.data.Messeger;
   //        }
   //    }, function (error) {
   //        $scope.status = 'Unable to load profile data: ' + error.message;
   //    });
   //};
   //function GetOrderById(orderId) {
   //    commonFactory.getOrderById(orderId).then(function (rsp) {
   //        if (rsp.data.Status) {
   //            $scope.BitcoinAccount.Amount = rsp.data.Result.Amount;
   //        }else {
   //            $scope.messeger = rsp.data.Msg;
   //        }
   //    }, function (error) {
   //        $scope.status = 'Unable to load profile data: ' + error.message;
   //    });
   //};
    $scope.checkPaymentBitcoin = function (Id) {
        $scope.processing = true,
        BitcoinFactory.CheckBitcoindPayment(Id).then(function successCallback(resp) {
            $scope.processing = false,
            $scope.title = "Check Payment",
            $scope.messeger = resp.data.Messeger;
            if (resp.data.Status) {
                $scope.popup('modal-message modal-success', 'Modal', 'ModalInstanceCtrl', 'sm');
            } else {
                $scope.popup('modal-message modal-warning', 'Modal', 'ModalInstanceCtrl', 'sm');
            }

        }, function (error) {
            $scope.processing = false,
            $scope.title = "Check Payment error",
            $scope.messeger = 'Unable to load status payment bitcoin data: ' + error.message;
            $scope.popup('modal-message modal-success', 'Modal', 'ModalInstanceCtrl', 'sm');
        });
    };
    //function getAccountBitCoin(UserId, Amount, Id, orderId) {
    //    BitcoinFactory.getAccountBitCoin(UserId, Amount, Id, orderId).then(
    //        function successCallback(resp) {
    //            if (resp.data.Status) {
    //                $scope.processing = false;
    //                $scope.BitcoinAccount.Address = resp.data.Result;
    //                $scope.BitcoinAccount.BCTransactionId = resp.data.BCTransactionId;
    //                $scope.Address = resp.data.Result;
    //                $scope.showQRCode = true;
    //            } else {
    //                $scope.messeger = resp.data.Messeger;
    //            }

    //        }, function errorCallback(resp) {
    //        })
    //};
    $scope.doInput = function (data) {
        $scope.processing = true;
        if (data.Amount > 0 && data.Address != '' && data.BCTransactionId) {
            $scope.showQRCode = true;
            getAccountBitCoin($localStorage.user.UserId, data.Amount, $scope.BitcoinAccount.BCTransactionId);
        } else {
            $scope.showQRCode = false;
        }
    };
    function GetBitcointoUSDRate(Bitcoin) {
        BitcoinFactory.GetBitcointoUSDRate(Bitcoin).then(function (response) {
            $scope.BitcoinAccount.Fee = response.data;
        }, function (error) {
            $scope.status = 'Unable to load rate data: ' + error.message;
        });
    };

}]);
app.controller('ModalInstanceCtrl', function ($scope, $modalInstance, messeger, title) {
    $scope.messeger = messeger,
    $scope.title = title;
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});